/*
********************************************************************************
* Copyright (C) 2021, xiang.D <dx_65535@163.com>.
* All right reserved.
*
* File Name   : iterator.h
* Author      :
* Version     : V1.0
* Description :
* Journal     : 2021-02-22 init v1.0
* Brief       : Blog: https://blog.csdn.net/qq_38750572?spm=1001.2014.3001.5343
* Others      :
********************************************************************************
*/

#ifndef __ITERATOR_H__
#define __ITERATOR_H__

#include "iteratorbase.h"
#include "aggregate.h"

#define ITER_LOGI(fmt, args...) printf(fmt, ##args)
#define ITER_LOGD(fmt, args...) //printf("%d Iterator D: " fmt, __LINE__, ##args)
#define ITER_LOGE(fmt, args...) printf("%d Iterator E: " fmt, __LINE__, ##args)

template<typename T>
class CIterator : public CIteratorBase <T>
{
public:
    CIterator(CAggregate<T>* aggregate) : mIndex(0), mAgg(aggregate)
    {

    }

    ~CIterator()
    {
        if (NULL != mAgg) {
            delete mAgg;
        }
    }

    T* First()
    {
        mIndex = 0;
        if (mAgg->mData[0]) {
            return &mAgg->mData[0];
        } else {
            ITER_LOGE("mAgg is NULL!\n");
            return NULL;
        }
    }

    T* Next()
    {
        mIndex++;
        if (mIndex < mAgg->mData.size()) {
            return &mAgg->mData[mIndex];
        } else {
            ITER_LOGD("mAgg is NULL!\n");
            return NULL;
        }
    }

    T *CurPos()
    {
        if (mAgg->mData[mIndex]) {
            return &mAgg->mData[mIndex];
        } else {
            ITER_LOGE("mAgg is NULL!\n");
            return NULL;
        }
    }

    bool IsEnd()
    {
        if (mIndex < mAgg->mData.size()) {
            return true;
        } else {
            ITER_LOGD("mIndex[%d] Size[%ld]\n", mIndex, mAgg->mData.size());
            return false;
        }
    }

private:
    unsigned int mIndex;
    CAggregate<T> *mAgg;
};

#endif